//
//  NearPlayTableViewController.m
//  ProductB
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "NearPlayTableViewController.h"
#import "MessageUrls.h"
#import "MessageMainVCTableViewCell.h"
#import "MessageMainModel.h"
#import "MessageWebViewController.h"
@interface NearPlayTableViewController ()
/**
 数据数组
 */
@property (nonatomic , strong) NSMutableArray *sourceArray;

@end

@implementation NearPlayTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册
    [(UITableView *)self.view registerClass:[MessageMainVCTableViewCell class] forCellReuseIdentifier:@"MessageMainVCTableViewCell"];
    //数据数组创建空间
    self.sourceArray = [[NSMutableArray alloc]init];
    //
    [self requestDataWithKey];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageMainVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageMainVCTableViewCell" forIndexPath:indexPath];
    MessageMainModel *model = _sourceArray[indexPath.row];
    [cell configureWithModel:model];
    //取消点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark--------返回行高----
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
#pragma mark--------请求数据的方法----
-(void)requestDataWithKey{
    [RequestManager initWithUrlString:kNearPlayUrl requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *dataArr = dataDic[@"data"][@"items"];
        for (NSDictionary *dic in dataArr) {
            MessageMainModel *model = [[MessageMainModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [(UITableView *)self.view reloadData];
        });
    } errorBlock:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}
#pragma mark--------tableView的点击事件----
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageWebViewController *webVc = [[MessageWebViewController alloc]init];
    MessageMainModel *model = _sourceArray[indexPath.row];
    webVc.webViewUrl = model.content_url;
    webVc.imgString = model.cover_image_url;
    [self.navigationController pushViewController:webVc animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
